package com.wonderful.compose.resize

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Popup
import androidx.compose.ui.window.PopupProperties
import androidx.compose.ui.zIndex
import com.wonderful.compose.ui.theme.ComposeLearnTheme


/**
 * Desc:
 * @Author: wonderful
 * @Time: 2024/2/18 15:42
 */
class AvoidResizeActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeLearnTheme {
                PopupTablePreview()
            }
        }
    }
}

data class SectionData(val sectionTitle: String, val items: List<String>)

@Composable
fun HeaderCell(title: String, isExpanded: Boolean, onHeaderClicked: () -> Unit) {
    Surface(color = Color.Blue) {
        Row(modifier = Modifier
            .clickable { onHeaderClicked() }
            .padding(vertical = 8.dp, horizontal = 16.dp)
            .fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Center) {
            Text(
                text = "hello $title",
                style = MaterialTheme.typography.headlineSmall,
                textAlign = TextAlign.Center,
                modifier = Modifier.padding(16.dp, 8.dp)
            )
            if (isExpanded) {
                Text(text = "▼", style = MaterialTheme.typography.headlineMedium)
            } else {
                Text(text = "▲", style = MaterialTheme.typography.headlineMedium)
            }
        }
    }
}

@Composable
fun RegularCell(title: String, modifier: Modifier = Modifier) {
    Surface(color = Color.Red) {
        Text(
            text = "hello $title",
            style = MaterialTheme.typography.bodySmall,
            modifier = modifier
                .padding(16.dp, 8.dp)
                .fillMaxWidth(),
            textAlign = TextAlign.Center
        )
    }
}

@Composable
fun Section(sectionData: SectionData) {
    var isExpanded by rememberSaveable {
        mutableStateOf(true)
    }

    Column {
        HeaderCell(
            title = sectionData.sectionTitle,
            isExpanded = isExpanded,
            onHeaderClicked = { isExpanded = !isExpanded }
        )
        if (isExpanded) {
            sectionData.items.onEachIndexed { _, item ->
                RegularCell(title = item)
            }
        }
    }
}

@Composable
fun ExpandableTableView(sectionList: List<SectionData>) {
    Column {
        sectionList.onEachIndexed { _, section ->
            Section(sectionData = section)
        }
    }
}

@Composable
fun PopupTable(sectionList: List<SectionData>) {
    val localDensity = LocalDensity.current
    // create element height in dp state
    var boxHeight by remember {
        mutableStateOf(0.dp)
    }

    var modifier = Modifier
        .width(300.dp)
        .padding(32.dp)
        .background(Color.White)
        .clip(RoundedCornerShape(4.dp))

    if (boxHeight == 0.dp) {
        modifier = modifier.onGloballyPositioned { layoutCoordinates ->
            boxHeight = with(localDensity) { layoutCoordinates.size.height.toDp() }
        }
    } else {
        modifier = modifier.height(boxHeight)
    }

    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.LightGray)
            .zIndex(10f), contentAlignment = Alignment.Center
    ) {
        Popup(alignment = Alignment.Center,
            properties = PopupProperties(excludeFromSystemGesture = true),
            onDismissRequest = {} // to dismiss on click outside
        ) {
            Box(
                modifier = modifier,
                contentAlignment = Alignment.TopCenter
            ) {
                Column {
                    ExpandableTableView(sectionList = sectionList)
                }
            }
        }
    }
}

@Preview(showBackground = true)
@Composable
fun PopupTablePreview() {
    val sectionData1 = SectionData("title1", listOf("1", "2", "3"))
    val sectionData2 = SectionData("title2", listOf("4", "5", "6"))
    val sectionList = listOf(sectionData1, sectionData2)
    ComposeLearnTheme {
        PopupTable(sectionList)
    }
}